How do i convert to Julain Date

by: davidjackson1955, 9 years ago


How can I convert a date (yyyy,mm,dd) to a Julian Date and write it to a csv file?
Here's the script I'm trying to write:

import csv
from jdcal import gcal2jd, jd2gcal

with open('/home/pi/Data/datesidc.csv') as csvfile:
    readCSV = csv.reader(csvfile, delimiter=',')
    for row in readCSV:
         gcal2jd(row[0],row[1],row[2])
#     print(row[0],row[1],row[2],)

And here's s sample of the date file:
[code'
yy,mm,dd,dec,spots
2015,01,01 , 2015.001 ,  104
2015,01,02 , 2015.004 ,  122
2015,01,03 , 2015.007 ,  102
2015,01,04 , 2015.010 ,  105
2015,01,05 , 2015.012 ,   88
2015,01,06 , 2015.015 ,   89
2015,01,07 , 2015.018 ,  103
2015,01,08 , 2015.021 ,   99
2015,01,09 , 2015.023 ,  111




You must be logged in to post. Please login or register an account.



Apparently datetime can convert to Julian. Never heard of it before now. https://pypi.python.org/pypi/DateTime/2.12.7 (search for julian).

I would HIGHLY recommend you stop using CSV reader at this point, use Pandas. From there, apply datetime's JulianDay to the date column.

Not sure how you're going to do that with using commas as the the delimiter and in the date, but maybe you have some data still in sqlite that is properly delimited.

-Harrison 9 years ago

You must be logged in to post. Please login or register an account.


Here is my revised step, which brings me a couple of steps closer:
(Script used, jdcal https://pypi.python.org/pypi/jdcal.]
Here's the script:


from jdcal import gcal2jd, jd2gcal
import pandas as pd

# sidc2015.csv has one record for each day of the year
df = pd.read_csv('/home/pi/Data/sidc2015.csv')

# df.set_index('Yr',inplace=T)
# print(df.head())

# loop through sidc2015.csc.
# Create csv file that has Julian Date as first field:
# Yr,Mo,Day,dt,spots,c,d,e
# JulianDate,Yr,Mo,Day,dt,spots,c,d,e
# And write it out to, "new.csv)
for row in df:
     jd = gcal2jd(2016,1,1)
     JD =(jd[0]+ jd[1])
     print(JD)


# Write new file
df.to_csv("/home/pi/Data/new.csv")


-davidjackson1955 9 years ago

You must be logged in to post. Please login or register an account.